package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.UserProject;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh
 *
 */
public class UserProjectDao extends DatabaseDAO {
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public List<UserProject> getRecordList (int userId)
	throws SQLException {
		ArrayList<UserProject> userProjectList = null;
		String query = "Select * from user_project where user_id =?";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			userProjectList = new ArrayList<UserProject>();
			while (rs.next()) {
				UserProject userProject = fillBean();
				userProjectList.add(userProject);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return userProjectList;
		
	}
	
	/**
	 * @return
	 * @throws SQLException
	 */
	private UserProject fillBean() throws SQLException{
		UserProject userProject = new UserProject();
		userProject.setUserId(rs.getInt("user_id"));
		userProject.setProjectId(rs.getInt("project_id"));
		return userProject;
	}
	
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public UserProject getRecord(int userId)
	throws SQLException {
		String query = "Select * from user_project where user_id = ? ";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				
				return fillBean();
			}
			return null;
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
		
	}
	
	/**
	 * @param userProject
	 * @return
	 * @throws SQLException
	 */
	public int updateRecord(UserProject userProject) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update user_project set project_id=? where user_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userProject.getProjectId());
			ps.setInt(2, userProject.getUserId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}

	/**
	 * @param userProject
	 * @return
	 * @throws SQLException
	 */
	public int insertRecord(UserProject userProject) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "insert into user_project (user_id, project_id) values( ? , ?)";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userProject.getUserId());
			ps.setInt(2, userProject.getProjectId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}

	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public boolean checkIsPresent(int userId) throws SQLException {
		getConnection();
		String query = "select 1 from user_project where user_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param userId
	 * @param projectId
	 * @return
	 * @throws SQLException
	 */
	public int deleteRecord(int userId , int projectId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from user_project where user_id =? and project_id = ?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			ps.setInt(2, projectId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
	
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public int deleteRecord(int userId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from user_project where user_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
}
